import 'package:flutter/material.dart';
import 'package:flutter_app/widgets/Button/index.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_app/views/Login/login.dart';
import 'package:flutter_app/views/BankCard/index.dart';
import 'package:flutter_app/views/Help/help.dart';
import 'package:flutter_app/views/Loan/list.dart';
import 'package:flutter_app/views/Settings/index.dart';

final Map profileRouteMap = {
  "My loan": LoanList(),
  "Bank card": BankCardList(),
  "Help": HelpCenter(),
  "Settings": Settings(),
};

class ListItem extends StatelessWidget {
  const ListItem({Key key}) : super(key: key);

  static const List<Map<String, String>> meList = [
    {
      "text": "My loan",
      "svg": "assets/images/icons/me_loan.svg",
    },
    {
      "text": "Bank card",
      "svg": "assets/images/icons/me_card.svg",
    },
    {
      "text": "Help",
      "svg": "assets/images/icons/me_zhinanzhen.svg",
    },
    {
      "text": "Settings",
      "svg": "assets/images/icons/me_setting.svg",
    },
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 1000,
      child: ListView.separated(
          itemBuilder: (context, index) {
            return GestureDetector(
              child: Container(
                color: Colors.transparent,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(meList[index]["text"],
                        style: TextStyle(color: Colors.black, fontSize: 15.0)),
                    SvgPicture.asset(
                      meList[index]["svg"],
                      height: 28,
                      width: 28,
                    ),
                  ],
                ),
              ),
              onTap: () {
                clickListItem(context, meList[index]["text"]);
              },
            );
          },
          separatorBuilder: (context, index) {
            return Divider();
          },
          itemCount: meList.length),
    );
  }

  clickListItem(BuildContext context, String text) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
      return profileRouteMap[text];
    }));
  }
}

class ProfilePage extends StatefulWidget {
  ProfilePage({Key key}) : super(key: key);

  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  final bool logined = true;

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.all(20),
      children: <Widget>[
        logined == true ? _renderAlreadyLogin() : _renderNoLogined(),
        ListItem(),
      ],
    );
  }

  Widget _renderNoLogined() {
    return Container(
      padding: EdgeInsets.only(top: 70, bottom: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(bottom: 10),
            width: 100,
            child: Button(
              text: 'Sign in',
              size: 'sm',
              radius: 50.0,
              onClick: () async {
                String result = await Navigator.push(
                  context,
                  new MaterialPageRoute(
                    builder: (context) => new Login(
                      params: {"title": 'we'},
                    ),
                  ),
                );

                if (result != null) {
                  Scaffold.of(context).showSnackBar(
                    new SnackBar(
                      content: new Text("$result"),
                      duration: const Duration(seconds: 1),
                    ),
                  );
                }
              },
            ),
          ),
          Row(
            children: <Widget>[
              Text('Don’t have an account?'),
              GestureDetector(
                child: Text(
                  'Register',
                  style: TextStyle(
                      color: Colors.blue, fontWeight: FontWeight.w700),
                ),
                onTap: () {
                  print('click register');
                },
              )
            ],
          )
        ],
      ),
    );
  }

  Widget _renderAlreadyLogin() {
    return Container(
      padding: EdgeInsets.only(top: 70, bottom: 30),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text('Welcome',
                  style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700)),
              Padding(
                padding: EdgeInsets.only(top: 15),
                child: Text('+63******2222',
                    style:
                        TextStyle(fontSize: 16, fontWeight: FontWeight.w700)),
              )
            ],
          ),
          Image.asset(
            "assets/images/atome_logo.png",
            width: 60,
          )
        ],
      ),
    );
  }
}
